package com.boss.salary.service.impl;

import com.boss.common.entity.PageDTO;
import com.boss.common.service.ICommonService;
import com.boss.common.util.DateUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.*;
import com.boss.salary.entity.*;
import com.boss.salary.enums.PersonFiledEnum;
import com.boss.salary.service.IAgencyInfoService;
import com.boss.salary.service.IAgencyVerifyService;
import com.boss.salary.service.IPersonOperateService;
import com.boss.salary.utils.CompareUtils;
import com.boss.salary.vo.ComparisonResultVo;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.sys.utils.SysUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class PersonOperateServiceImpl implements IPersonOperateService {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(PersonOperateServiceImpl.class);

    private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    private IPersonOperateDao iPersonOperateDao;

    @Autowired
    private IPersonInfoDao iPersonInfoDao;

    @Autowired
    private IPersonExtDao iPersonExtDao;

    @Autowired
    private ISalaryMonthEndDao iSalaryMonthEndDao;

    @Autowired
    private IAgencyVerifyDao agencyVerifyDao;

    @Autowired
    private IAgencyInfoDao agencyInfoDao;

    @Autowired
    private IPersonHistoryDao personHistoryDao;

    @Autowired
    private IAgencyVerifyService iAgencyVerifyService;

    @Autowired
    private ISalaryMonthBeginDao iSalaryMonthBeginDao;
    @Autowired
    private ICommonService iCommonService;
    @Autowired
    private IAgencyInfoService agencyInfoService;

    @Override
    public PageDTO<PersonOperate> queryPersonOperatePage(PaginationDTO paginationDTO) throws AppRuntimeException {
        List<PersonOperate> list = iPersonOperateDao.queryPersonOperatePage(paginationDTO);
        int total = iPersonOperateDao.queryPersonOperateTotal();
        PageDTO<PersonOperate> pageDTO = new PageDTO<>();
        pageDTO.setRows(list);
        pageDTO.setTotal(total);
        return pageDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePersonOperate(PersonOperate personOperate) {
        if (StringUtils.isEmpty(personOperate.getPersonId())) {
            //新增
            int operateNum1=0;
            int infoNum = iPersonOperateDao.verifyIdenNoAndBankCardRepeatFromInfo(
                    personOperate.getIdenNo(), personOperate.getSalaCardNo(), null,personOperate.getMofDivCode());
//            int operateNum = iPersonOperateDao.verifyIdenNoAndBankCardRepeatFromOperate(
//                    personOperate.getIdenNo(), personOperate.getSalaCardNo(), null,personOperate.getMofDivCode());
            if(personOperate.getOperateCode().equals("add")){
                 operateNum1 = iPersonOperateDao.verifyIdenNoAndBankCardRepeatFromOperateCheck(
                        personOperate.getIdenNo(),personOperate.getMofDivCode());

            }
            if (infoNum > 0) {
                throw new AppRuntimeException("当前身份证号或银行卡号已被使用！");
            }
            if(operateNum1> 0){
                throw new AppRuntimeException("存在待调入信息，请执行调入操作！");
            }
        } else if(!SystemDictConstant.DELETE_PERSON.equals(personOperate.getOperateCode())){
            //编辑
            int operateNum1=0;
            int infoNum = iPersonOperateDao.verifyIdenNoAndBankCardRepeatFromInfo(
                    personOperate.getIdenNo(), personOperate.getSalaCardNo(), personOperate.getPersonId(),personOperate.getMofDivCode());
//            int operateNum = iPersonOperateDao.verifyIdenNoAndBankCardRepeatFromOperate(
//                    personOperate.getIdenNo(), personOperate.getSalaCardNo(), personOperate.getPersonId(),personOperate.getMofDivCode());
            if(personOperate.getOperateCode().equals("add")){
                operateNum1 = iPersonOperateDao.verifyIdenNoAndBankCardRepeatFromOperateCheck(
                        personOperate.getIdenNo(),personOperate.getMofDivCode());
            }
            if (infoNum> 0) {
                throw new AppRuntimeException("当前身份证号或银行卡号已被使用！");
            }
            if(operateNum1> 0){
                throw new AppRuntimeException("存在待调入信息，请执行调入操作！");
            }

        }
        // 如果是待调入人员
        if (SystemDictConstant.IN_PERSON.equals(personOperate.getOperateCode()) && StringUtils.isEmpty(personOperate.getVerifyState())) {
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            iPersonOperateDao.updatePersonOperate(personOperate);
        }
        // 新增/修改人员
        else {
            if (StringUtils.isEmpty(personOperate.getPersonId())) {
                personOperate.setPersonId(SysUtils.generateGuid());
            }
            personOperate.setId(SysUtils.generateGuid());
            personOperate.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setStartDate(String.valueOf(DateUtils.getCurrentTimeStamp()));
            if(SystemDictConstant.EDIT_PERSON.equals(personOperate.getOperateCode())){
                personOperate.setInFromAgency(null);
                personOperate.setOutToAgency(null);
            }

            if (SystemDictConstant.ADD_PERSON.equals(personOperate.getOperateCode())) {
                personOperate.setIdenIsVerify(String.valueOf(SystemDictConstant.IS_FALSE));
                personOperate.setIdenVerifyState(SystemDictConstant.NO_SUBMIT);
            }
            // 未删除
            personOperate.setIsDeleted(SystemDictConstant.NOT_DELETED);
            // 审核通过修改为未上报
            if (StringUtils.isEmpty(personOperate.getVerifyState()) ||
                    SystemDictConstant.IS_AUDIT.equals(personOperate.getVerifyState())) {
                personOperate.setVerifyState(SystemDictConstant.NO_SUBMIT);
            }
            personOperate.setIsReal(SystemDictConstant.IS_FALSE);
            iPersonOperateDao.savePersonOperate(personOperate);
        }
        List<PersonOperate> list=new ArrayList<>();
        list.add(personOperate);
        try {
            personApproved(list);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePersonOperateList(List<PersonOperate> personOperates) {
        try {
            personApproved(personOperates);//初始化人员信息
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePersonOperate(PersonOperate personOperate) {
        personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        iPersonOperateDao.updatePersonOperate(personOperate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePersonOperate(String id) {
        iPersonOperateDao.deletePersonOperate(id);
    }

    @Override
    public PageDTO<PersonOperate> getPersonOperatePage(PaginationDTO paginationDTO, Map<String, Object> queryParam) throws AppRuntimeException {
        List<PersonOperate> personOperatePage = iPersonOperateDao.getPersonOperatePage(paginationDTO, queryParam);
        int total = iPersonOperateDao.getPersonOperateTotal(queryParam);
        PageDTO<PersonOperate> pageDTO = new PageDTO<>();
        pageDTO.setTotal(total);
        pageDTO.setRows(personOperatePage);
        return pageDTO;
    }

    @Override
    public PageDTO<PersonOperate> getPersonChangeList(PaginationDTO paginationDTO, Map<String, Object> queryParam) throws AppRuntimeException {
        List<PersonOperate> personOperatePage = iPersonOperateDao.getPersonChangeListPage(paginationDTO, queryParam);
        int total = iPersonOperateDao.getPersonChangeListTotal(queryParam);
        //判断是否是新增人员，新增人员不显示修改操作
        Map<String,PersonOperate> addOperateMap=new HashMap<>();//addPerson
        Map<String,PersonOperate> operateMap=new HashMap<>();//除新增所有变动信息
        for(PersonOperate p:personOperatePage){
            if(SystemDictConstant.ADD_PERSON.equals(p.getOperateCode())) {//是新增人员则添加到addOperateMap中
                addOperateMap.put(p.getPersonId(), p);
            }else{//否则添加到人员则添加到operateMap中
                operateMap.put(p.getPersonId(), p);
            }
        }
        int addPersonEditNum=0;
        if(!addOperateMap.isEmpty()){
            for(String personId:addOperateMap.keySet()){
                PersonOperate pchange=operateMap.get(personId);
                if(pchange!=null&&SystemDictConstant.EDIT_PERSON.equals(pchange.getOperateCode())){
                    //除新增外存在信息并且信息为修改
                    personOperatePage.remove(pchange);
                    addPersonEditNum++;
                }
            }
        }
        PageDTO<PersonOperate> pageDTO = new PageDTO<>();
        pageDTO.setTotal(total-addPersonEditNum);
        pageDTO.setRows(personOperatePage);
        return pageDTO;
    }

    @Override
    public PageDTO<PersonOperate> getPersonInfoChangeOperatePage(PaginationDTO paginationDTO, Map<String, Object> queryParam) throws AppRuntimeException {
        String agencyId = String.valueOf(queryParam.get("agencyId"));
//        查询当前月份，送审状态及工资变动状态
        Map<String,Object> stateMap = agencyVerifyDao.selectStateByAgencyId(agencyId,"salary_month");
//        当月月份
        String salaryMonth = String.valueOf(stateMap.get("salary_month"));
        queryParam.put("salaryMonth", salaryMonth);
        List<PersonOperate> personOperatePage = iPersonOperateDao.getPersonInfoChangeOperatePage(paginationDTO, queryParam);
        int total = iPersonOperateDao.getPersonInfoChangeOperateTotal(queryParam);
        PageDTO<PersonOperate> pageDTO = new PageDTO<>();
        pageDTO.setTotal(total);
        pageDTO.setRows(personOperatePage);
        return pageDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void checkPassPersonOperate(List<PersonOperate> list) {
        for (PersonOperate personOperate : list) {
            personOperate.setIdenIsVerify(SystemDictConstant.IS_SUBMIT);
            personOperate.setId(SysUtils.generateGuid());
            personOperate.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setIsDeleted(SystemDictConstant.NOT_DELETED);
            personOperate.setOperateCode(SystemDictConstant.CHECK_IDEN_NO);
            iPersonOperateDao.savePersonOperate(personOperate);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void undoCheckPersonOperate(List<PersonOperate> list) {
        for (PersonOperate personOperate : list) {
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setIsDeleted(SystemDictConstant.IS_DELETED);
            iPersonOperateDao.updatePersonOperate(personOperate);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revokeOperate(List<PersonOperate> list) {
        for (PersonOperate personOperate : list) {
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setIsDeleted(SystemDictConstant.IS_DELETED);
            iPersonOperateDao.updatePersonOperate(personOperate);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void rejectOperate(List<PersonOperate> list) {
        for (PersonOperate personOperate : list) {
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setVerifyState(SystemDictConstant.IS_REJECT);
            iPersonOperateDao.updatePersonOperate(personOperate);
        }
    }

    @Override
    public void personApproved(List<PersonOperate> list) throws ParseException {
        String agencyId = list.get(0).getAgencyId();
        Map<String,Object> stateMap = agencyVerifyDao.selectStateByAgencyId(agencyId, "salary_month");
        String salaryMonth = String.valueOf(stateMap.get("salary_month"));

        for (PersonOperate personOperate : list) {
            // 获取生效日期
            String startDate = personOperate.getStartDate();
            LocalDateTime localDateTime = LocalDateTime.now();
            if (!StringUtils.isEmpty(startDate)) {
                Date date = df.parse(startDate);
                ZoneId zoneId = ZoneId.systemDefault();
                localDateTime = LocalDateTime.ofInstant(date.toInstant(), zoneId);
            }
            // 将操作表中的数据修改为已通过审核
            personOperate.setIdenIsVerify(String.valueOf(SystemDictConstant.IS_FALSE));
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setVerifyState(SystemDictConstant.IS_AUDIT);
//            // 如果是离休人员或者是到生效时间
//            if (SystemDictConstant.QUIT_PERSON.equals(personOperate.getState()) ||
//                    !localDateTime.isAfter(LocalDateTime.now())) {
            personOperate.setIsReal(SystemDictConstant.IS_TRUE);
//            }
            personOperate.setSalaryMonth(salaryMonth);
            iPersonOperateDao.updatePersonOperate(personOperate);
            // 如果是离休人员或者当前时间大于等于生效日期，则更新人员表数据
//            if (SystemDictConstant.QUIT_PERSON.equals(personOperate.getState()) ||
//                    !localDateTime.isAfter(LocalDateTime.now())) {
                // 更新人员表数据
                PersonInfo personInfo = new PersonInfo();
                personInfo.setId(personOperate.getPersonId());
                personInfo.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                personInfo.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                personInfo.setPersonName(personOperate.getPersonName());
                personInfo.setSexCode(personOperate.getSexCode());
                personInfo.setIdenTypeCode(personOperate.getIdenTypeCode());
                personInfo.setIdenNo(personOperate.getIdenNo());
                personInfo.setNation(personOperate.getNation());
                personInfo.setNatCode(personOperate.getNatCode());
                personInfo.setState(personOperate.getState());
                personInfo.setBirthday(personOperate.getBirthday());
                personInfo.setWorkInitDate(personOperate.getWorkInitDate());
                personInfo.setFundmode(personOperate.getFundmode());
                personInfo.setIsDeleted(SystemDictConstant.NOT_DELETED);

                PersonExt personExt = new PersonExt(personOperate);//通过操作表赋值

                // 如何是新增人，将操作表数据添加到人员信息表
                if (SystemDictConstant.ADD_PERSON.equals(personOperate.getOperateCode())) {
                    personExt.setIsStopSalary(SystemDictConstant.IS_FALSE);
                    personExt.setId(SysUtils.generateGuid());
                    personInfo.setIdenNo(personOperate.getIdenNo());
                    personInfo.setMofDivCode(personOperate.getMofDivCode());
                    iPersonInfoDao.savePersonInfo(personInfo);
                    iPersonExtDao.savePersonExt(personExt);
                    // 单位审核表增加离休人数
                    if (SystemDictConstant.QUIT_PERSON.equals(personOperate.getState())) {
                        iAgencyVerifyService.updateAddQuitPersonNum(personOperate.getAgencyId());
                    }
                    // 单位审核表增加在职人数
                    else if (SystemDictConstant.REGISTER_PERSON.equals(personOperate.getState())) {
                        iAgencyVerifyService.updateAddPersonNum(personOperate.getAgencyId());
                    }
                    // 单位审核表增加变动人数
                    //iAgencyVerifyService.updateChangePersonNum(personOperate.getAgencyId());
                    // 工资表增加人员数据
                    Object[] params = new Object[]{
                            SysUtils.generateGuid(), String.valueOf(DateUtils.getCurrentTimeStamp()),
                            String.valueOf(DateUtils.getCurrentTimeStamp()), personOperate.getAgencyId(),
                            personOperate.getPersonId(), SystemDictConstant.IS_FALSE,personOperate.getMofDivCode()
                    };
                  //  iSalaryMonthEndDao.addPersonSalary(params);//原新增人员需添加工资信息，现功能是在新增人员时直接编辑工资，不需要再此处插入如见
                }
                // 如果是调出，在操作表添加去向单位数据，并更新人员信息表数据
                else if (SystemDictConstant.OUT_PERSON.equals(personOperate.getOperateCode())) {
                    Map<String,Object> totalMap=new HashMap<>();
                    totalMap.put("agencyId",agencyId);
                    int currentCount=iPersonInfoDao.queryPersonInfoTotal(totalMap);//当前单位在册人数
                    // 更新操作表调出数据为已审核通过
                    personOperate.setVerifyState(SystemDictConstant.IS_AUDIT);
                    iPersonOperateDao.updatePersonOperate(personOperate);
                    agencyId = personOperate.getAgencyId();

                    // 保存操作表数据
                    String outAgencyId = personOperate.getOutToAgency();
                    AgencyInfo agencyInfo=agencyInfoService.getNameById(outAgencyId);
                    personOperate.setId(SysUtils.generateGuid());
                    personOperate.setAgencyId(outAgencyId);
                    personOperate.setDeptId(null);
                    personOperate.setOutToAgency(null);
                    personOperate.setAgencyCode(agencyInfo.getAgencyCode());
                    personOperate.setInFromAgency(agencyId);
                    personOperate.setOperateCode(SystemDictConstant.IN_PERSON);
                    personOperate.setVerifyState(null);
                    personOperate.setIsDeleted(SystemDictConstant.NOT_DELETED);
                    personOperate.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                    personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                    iPersonOperateDao.savePersonOperate(personOperate);
                    // 将原有的人员信息标记删除
                    iPersonInfoDao.deletePersonInfo(personOperate.getPersonId());
                    personExt.setIsDeleted(SystemDictConstant.IS_DELETED);
                    iPersonExtDao.updatePersonExt(personExt);
                    // 单位审核表减人数
                    iAgencyVerifyService.updateDeletePersonNum(agencyId);
                    // 单位审核表增加变动人数
                   // iAgencyVerifyService.updateChangePersonNum(personOperate.getAgencyId());
                    // 删除工资表数据
                    //iSalaryMonthEndDao.deletePersonSalary(personOperate.getPersonId());
                    String agencyIds = "";
                    //修改工资表数据
                    iSalaryMonthEndDao.updatePersonSalary(personOperate.getPersonId(), agencyIds);
                    if(currentCount==1){
                        AgencyInfo deleteagencyInfo=agencyInfoService.getNameById(agencyId);
                        deleteagencyInfo.setIsDeleted(SystemDictConstant.IS_DELETED);
                        deleteagencyInfo.setUpdateTime(SysUtils.getCurrentTime());
                        agencyInfoService.updateAgencyInfoIsPaymentAgency(deleteagencyInfo);//若单位只剩一人并进行调出操作则删除统发单位
                    }
                }
                // 如果是消减，将人员表标记删除
                else if (SystemDictConstant.DELETE_PERSON.equals(personOperate.getOperateCode())) {
                    Map<String,Object> totalMap=new HashMap<>();
                    totalMap.put("agencyId",agencyId);
                    int currentCount=iPersonInfoDao.queryPersonInfoTotal(totalMap);//当前单位在册人数
                    personInfo.setIsDeleted(SystemDictConstant.IS_DELETED);
                    iPersonInfoDao.deletePersonInfo(personOperate.getPersonId());
                    personExt.setIsDeleted(SystemDictConstant.IS_DELETED);
                    iPersonExtDao.updatePersonExt(personExt);
                    // 单位审核表减人数
                    iAgencyVerifyService.updateDeletePersonNum(personOperate.getAgencyId());
                    // 单位审核表增加变动人数
                   // iAgencyVerifyService.updateChangePersonNum(personOperate.getAgencyId());
                    // 删除工资表数据
                    iSalaryMonthEndDao.deletePersonSalary(personOperate.getPersonId());
                    if(currentCount==1){
                        AgencyInfo deleteagencyInfo=agencyInfoService.getNameById(agencyId);
                        deleteagencyInfo.setIsDeleted(SystemDictConstant.IS_DELETED);
                        deleteagencyInfo.setUpdateTime(SysUtils.getCurrentTime());
                        agencyInfoService.updateAgencyInfoIsPaymentAgency(deleteagencyInfo);//若单位只剩一人并进行消减操作则删除统发单位
                    }
                }
                // 如果是调入，将人员表数据的单位信息更新为调入单位的id
                else if (SystemDictConstant.IN_PERSON.equals(personOperate.getOperateCode())) {
                    // 更新人员表数据
                    personInfo.setIsDeleted(SystemDictConstant.NOT_DELETED);
                    iPersonInfoDao.updatePersonInfo(personInfo);
                    // 更新人员拓展表数据
                    String outAgencyId = personOperate.getAgencyId();
                    AgencyInfo agencyInfo=agencyInfoService.getNameById(outAgencyId);
                    personExt.setDeptId(null);
                    personExt.setAgencyId(outAgencyId);
                    personExt.setAgencyCode(agencyInfo.getAgencyCode());
                    personExt.setVerifyState(SystemDictConstant.NO_SUBMIT);
                    personExt.setIsStopSalary(SystemDictConstant.IS_FALSE);
                    personExt.setIsDeleted(SystemDictConstant.NOT_DELETED);
                    iPersonExtDao.updatePersonExt(personExt);
                    // 修改工资表人的单位id
                    iSalaryMonthEndDao.updateAgencyIdByPersonId(personOperate.getPersonId(), personOperate.getAgencyId(),
                            String.valueOf(DateUtils.getCurrentTimeStamp()));
                    // 单位审核表加人数
                    iAgencyVerifyService.updateAddPersonNum(personOperate.getAgencyId());
                    // 单位审核表增加变动人数
                   // iAgencyVerifyService.updateChangePersonNum(personOperate.getAgencyId());
                    // 工资表增加人员数据
                   /* Object[] params = new Object[] {
                            SysUtils.generateGuid(), String.valueOf(DateUtils.getCurrentTimeStamp()),
                            String.valueOf(DateUtils.getCurrentTimeStamp()), personOperate.getAgencyId(),
                            personOperate.getPersonId(), SystemDictConstant.IS_FALSE
                    };
                    iSalaryMonthEndDao.addPersonSalary(params);*/
                    //修改工资表信息
                    iSalaryMonthEndDao.updatePersonSalary(personOperate.getPersonId(), personOperate.getAgencyId());
                }
                // 更新人员信息数据
                else {
                    iPersonInfoDao.updatePersonInfo(personInfo);
                    iPersonExtDao.updatePersonExt(personExt);
                }
//            }

        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void rejectPersonApproved(PersonOperate personOperate) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        String operateCode = personOperate.getOperateCode();
        String personId = personOperate.getPersonId();
        String agencyId = personOperate.getAgencyId();
        String operateId = personOperate.getId();
        String state = personOperate.getState();


        String mof_div_code=personOperate.getMofDivCode();
//        上月月份
        String lastSalaryMonth = LocalDate.parse(personOperate.getSalaryMonth()+"-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"))
                .minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        if(SystemDictConstant.ADD_PERSON.equals(operateCode)){
//            人员新增
            iPersonInfoDao.physicalDeleteByPersonId(personId);
            iPersonExtDao.physicalDeleteByPersonId(personId);
            //iPersonOperateDao.updateVerifyStateById(operateId, SystemDictConstant.NO_SUBMIT);
            iPersonOperateDao.physicalDeleteById(operateId);//编辑即生效，撤销生效时直接删除该操作
            Map<String,String> changeMap = new HashMap<>();
            //changeMap.put("is_salary_change", "-1");
            if(SystemDictConstant.REGISTER_PERSON.equals(state)){
//                在职
                changeMap.put("add_person_num", "-1");
            }else{
//                离休
                changeMap.put("add_quit_person_num", "-1");
            }
            agencyVerifyDao.updateChangeNum(agencyId, changeMap);
            iSalaryMonthEndDao.deletePersonSalary(personId);
        }else if(SystemDictConstant.DELETE_PERSON.equals(operateCode)){
//            人员消减
            int infoNum = iPersonOperateDao.verifyIdenNoAndBankCardRepeatFromInfo(
                    personOperate.getIdenNo(), personOperate.getSalaCardNo(), null,personOperate.getMofDivCode());
            if (infoNum> 0) {
                throw new AppRuntimeException("当前消减人员已通过新增方式录入信息，无法执行撤销消减操作！");
            }
            int operateNum = iPersonOperateDao.verifyIdenNoAndBankCardRepeatFromOperateCheck(
                    personOperate.getIdenNo(),personOperate.getMofDivCode());
            if(operateNum> 0){
                throw new AppRuntimeException("当前操作人身份证号存在待调入信息，请执行调入操作！");
            }
            PersonInfo personInfo = new PersonInfo();
            personInfo.setId(personId);
            personInfo.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personInfo.setIsDeleted(SystemDictConstant.NOT_DELETED);
            iPersonInfoDao.updatePersonInfo(personInfo);
            PersonExt personExt = new PersonExt();
            personExt.setPersonId(personId);
            personExt.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personExt.setIsDeleted(SystemDictConstant.NOT_DELETED);
            iPersonExtDao.updatePersonExt(personExt);
            //iPersonOperateDao.updateVerifyStateById(operateId, SystemDictConstant.NO_SUBMIT);
            iPersonOperateDao.physicalDeleteById(operateId);//编辑即生效，撤销生效时直接删除该操作
            Map<String,String> changeMap = new HashMap<>();
           // changeMap.put("is_salary_change", "-1");
            changeMap.put("delete_person_num", "-1");
            agencyVerifyDao.updateChangeNum(agencyId, changeMap);
            SalaryMonthEnd salaryMonthEnd =  iSalaryMonthBeginDao.selectSalaryBeginByPersonId(personId);
            //SalaryHistory salaryHistory = salaryHistoryDao.selectHistorySalaryByPersonId(personId, agencyId, lastSalaryMonth);
            if(StringUtils.isEmpty(salaryMonthEnd)){
                // 工资表增加人员数据
                Object[] params = new Object[]{
                        SysUtils.generateGuid(), String.valueOf(DateUtils.getCurrentTimeStamp()),
                        String.valueOf(DateUtils.getCurrentTimeStamp()), agencyId, personId, SystemDictConstant.IS_FALSE,mof_div_code
                };
                iSalaryMonthEndDao.addPersonSalary(params);
            }else{
               // SalaryMonthEnd salaryMonthEnd = new SalaryMonthEnd(salaryHistory);
                salaryMonthEnd.setColumnNames("s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16,s17,s18,s19,s20," +
                        "s21,s22,s23,s24,s25,s26,s27,s28,s29,s30,s31,s32,s33,s34,s35,s36,s37,s38," +
                        "s39,s40,s41,s42,s43,s44,s45,s46,s47,s48,s49,s50,s51,s52,s53,s54,s55,s56," +
                        "s57,s58,s59,s60,s61,s62,s63,s64,s65,s66,s67,s68,s69,s70,s71,s72,s73,s74," +
                        "s75,s76,s77,s78,s79,s80,s81,s82,s83,s84,s85,s86,s87,s88,s89,s90,s91,s92," +
                        "s93,s94,s95,s96,s97,s98,s99,s100");
                iSalaryMonthEndDao.insertSalaryMonthEndDao(salaryMonthEnd);//修改原从历史表查询数据为从月初表查询
            }
        }else if(SystemDictConstant.IN_PERSON.equals(operateCode)){
//            人员调入
            PersonInfo personInfo = new PersonInfo();
            personInfo.setId(personId);
            personInfo.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personInfo.setIsDeleted(SystemDictConstant.IS_DELETED);
            iPersonInfoDao.updatePersonInfo(personInfo);
            PersonExt personExt = new PersonExt();
            personExt.setPersonId(personId);
            personExt.setAgencyId(personOperate.getInFromAgency());
            personExt.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personExt.setIsDeleted(SystemDictConstant.IS_DELETED);
            personExt.setAgencyCode(agencyInfoDao.getCodeByAgencyId(personOperate.getInFromAgency()));
            iPersonExtDao.updatePersonExt(personExt);
            Map<String,String> changeMap = new HashMap<>();
          //  changeMap.put("is_salary_change", "-1");
            if(SystemDictConstant.REGISTER_PERSON.equals(state)){
//                在职
                changeMap.put("add_person_num", "-1");
            }else{
//                离休
                changeMap.put("add_quit_person_num", "-1");
            }
            agencyVerifyDao.updateChangeNum(agencyId, changeMap);
            iPersonOperateDao.updateVerifyStateById(operateId, null);
//            删除点击调入时生成的数据
            iPersonOperateDao.physicalDeleteById(operateId);
            String agencyIdStr = "";
            //修改工资表数据
            iSalaryMonthEndDao.updatePersonSalary(personId, agencyIdStr);
        }else if(SystemDictConstant.OUT_PERSON.equals(operateCode)){
//            人员调出
//            判断是否已经调入
            int count = iPersonOperateDao.selectInPersonNumByPersonId(personId, personOperate.getAgencyId(), personOperate.getSalaryMonth());
            if(count < 2){
                PersonInfo personInfo = new PersonInfo();
                personInfo.setId(personId);
                personInfo.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                personInfo.setIsDeleted(SystemDictConstant.NOT_DELETED);
                iPersonInfoDao.updatePersonInfo(personInfo);
                PersonExt personExt = new PersonExt();
                personExt.setPersonId(personId);
                personExt.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                personExt.setIsDeleted(SystemDictConstant.NOT_DELETED);
                iPersonExtDao.updatePersonExt(personExt);
                //iPersonOperateDao.updateVerifyStateById(operateId, SystemDictConstant.NO_SUBMIT);
                iPersonOperateDao.physicalDeleteById(operateId);//编辑即生效，撤销生效时直接删除该操作
                Map<String,String> changeMap = new HashMap<>();
               // changeMap.put("is_salary_change", "-1");
                changeMap.put("delete_person_num", "-1");
                agencyVerifyDao.updateChangeNum(agencyId, changeMap);
                iPersonOperateDao.physicalDeleteInPerson(personId, personOperate.getAgencyId(), personOperate.getSalaryMonth());
                iSalaryMonthEndDao.updatePersonSalary(personId, personOperate.getAgencyId());
            }else{
                throw new AppRuntimeException("当前人员已在目标单位调入，不可撤销审核！");
            }
        }else{
//            人员编辑
           // iPersonOperateDao.updateVerifyStateById(operateId, SystemDictConstant.NO_SUBMIT);
            iPersonOperateDao.physicalDeleteById(operateId);//编辑即生效，撤销生效时直接删除该操作

//            PersonHistory personHistory = personHistoryDao.selectHistoryPersonExtByPersonId(personId, lastSalaryMonth);
            PersonOperate personHistory = iPersonOperateDao.selectHistoryOperateByPersonId(personId,operateId);
            if(StringUtils.isEmpty(personHistory)){
                PersonOperate operate = iPersonOperateDao.selectHistoryAddOperateByPersonId(personId);
                PersonExt personExt = new PersonExt(operate,personId);
                PersonInfo pi=new PersonInfo();
                pi.setIdenNo(personExt.getIdenNo());
                pi.setPersonName(personExt.getPersonName());
                pi.setId(personId);
                iPersonInfoDao.updatePersonInfo(pi);
                iPersonExtDao.updatePersonExt(personExt);

            }else{
                PersonExt personExt = new PersonExt(personHistory,personId);
                PersonInfo pi=new PersonInfo();
                pi.setIdenNo(personExt.getIdenNo());
                pi.setPersonName(personExt.getPersonName());
                pi.setId(personId);
                iPersonInfoDao.updatePersonInfo(pi);
                iPersonExtDao.updatePersonExt(personExt);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revokeVerify(List<PersonOperate> list) {
        for (PersonOperate personOperate : list) {
            personOperate.setVerifyState(SystemDictConstant.IS_SUBMIT);
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            iPersonOperateDao.updatePersonOperate(personOperate);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePersonOperateID(PersonOperate personOperate) {
        personOperate.setId(SysUtils.generateGuid());
        personOperate.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        personOperate.setIsDeleted(SystemDictConstant.NOT_DELETED);
        personOperate.setOperateCode(SystemDictConstant.CHECK_IDEN_NO);
        iPersonOperateDao.savePersonOperate(personOperate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void undoCheckPersonOperateID(PersonOperate personOperate) {
        personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        personOperate.setIsDeleted(SystemDictConstant.IS_DELETED);
        iPersonOperateDao.updatePersonOperate(personOperate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void forReviewPersonOperate(List<PersonOperate> list) {
        for (PersonOperate personOperate : list) {
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setIdenVerifyState(SystemDictConstant.IS_SUBMIT);
            iPersonOperateDao.updatePersonOperate(personOperate);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitPersonOperateList(List<PersonOperate> list) {
        for (PersonOperate personOperate : list) {
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setVerifyState(SystemDictConstant.IS_SUBMIT);
            personOperate.setIdenVerifyState(SystemDictConstant.NO_SUBMIT);
            personOperate.setIsReal(SystemDictConstant.IS_FALSE);
            iPersonOperateDao.updatePersonOperate(personOperate);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void undoForReviewPersonOperate(List<PersonOperate> list) {
        for (PersonOperate personOperate : list) {
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setIdenVerifyState(SystemDictConstant.NO_SUBMIT);
            iPersonOperateDao.updatePersonOperate(personOperate);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void undoSubmitPersonOperateList(List<PersonOperate> list) {
        for (PersonOperate personOperate : list) {
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setVerifyState(SystemDictConstant.NO_SUBMIT);
            iPersonOperateDao.updatePersonOperate(personOperate);
        }
    }

    @Override
    public Map<String, Object> getStateNum(Map<String, Object> queryParam) {
        return iPersonOperateDao.getStateNum(queryParam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditPassPersonOperate(List<PersonOperate> list) {
        for (PersonOperate personOperate : list) {
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setIdenVerifyState(SystemDictConstant.IS_AUDIT);
            iPersonOperateDao.updatePersonOperate(personOperate);
            //跟新perseninfo
            PersonInfo personInfo = new PersonInfo();
            personInfo.setIdenNo(personOperate.getIdenNo());
            personInfo.setId(personOperate.getPersonId());
            personInfo.setIdenIsVerify(personOperate.getIdenIsVerify());
            personInfo.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            iPersonInfoDao.updatePersonInfo(personInfo);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void undoAuditPassPersonOperate(List<PersonOperate> list) {
        for (PersonOperate personOperate : list) {
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setIdenVerifyState(SystemDictConstant.IS_REJECT);
            iPersonOperateDao.updatePersonOperate(personOperate);

        }
    }

    @Override
    public List<ComparisonResultVo> getPersonOperateRecordType(String agencyId, String personId,String personState) {
        List<ComparisonResultVo> resultVoList = new ArrayList<>();
        List<PersonOperate> operateList=null;
        if("待调入".equals(personState)){
            operateList=iPersonOperateDao.getPersonOperatePersonState(agencyId,personId,personState);
        }else{
            // 获取人员变更记录
            operateList = iPersonOperateDao.getPersonOperateRecord(agencyId, personId, null,null);
        }

        // 获取不同的操作集合
        for (int i = 0; i < operateList.size(); i++) {
            boolean isExist = false;
            PersonOperate personOperate = operateList.get(i);
            // 如何之前添加过该操作code,不添加到数组集合中
//            for (ComparisonResultVo comparisonResultVo1 : resultVoList) {
//                if (comparisonResultVo1.getField().equals(personOperate.getOperateCode())) {
//                    isExist = true;
//                    break;
//                }
//            }
            if (!isExist && !SystemDictConstant.CHECK_IDEN_NO.equals(personOperate.getOperateCode())) {
                ComparisonResultVo comparisonResultVo = new ComparisonResultVo();
                if (SystemDictConstant.ADD_PERSON.equals(personOperate.getOperateCode())) {
                    comparisonResultVo.setField(SystemDictConstant.ADD_PERSON);
                    comparisonResultVo.setFieldNotes("新增");
                } else if (SystemDictConstant.IN_PERSON.equals(personOperate.getOperateCode())&&"2".equals(personOperate.getVerifyState())) {
                    comparisonResultVo.setField(SystemDictConstant.IN_PERSON);
                    comparisonResultVo.setFieldNotes("调入");
                } else if (SystemDictConstant.OUT_PERSON.equals(personOperate.getOperateCode())) {
                    comparisonResultVo.setField(SystemDictConstant.OUT_PERSON);
                    comparisonResultVo.setFieldNotes("调出");
                } else if (SystemDictConstant.DELETE_PERSON.equals(personOperate.getOperateCode())) {
                    comparisonResultVo.setField(SystemDictConstant.DELETE_PERSON);
                    comparisonResultVo.setFieldNotes("消减");
                } else if (SystemDictConstant.EDIT_PERSON.equals(personOperate.getOperateCode())) {
                    comparisonResultVo.setField(SystemDictConstant.EDIT_PERSON);
                    comparisonResultVo.setFieldNotes("修改");
                }else if(SystemDictConstant.IN_PERSON.equals(personOperate.getOperateCode())&&StringUtils.isEmpty(personOperate.getVerifyState())){
                    comparisonResultVo.setField("待调入");
                    comparisonResultVo.setFieldNotes("待调入");
                }else if(SystemDictConstant.STOP_PERSON.equals(personOperate.getOperateCode())){
                    comparisonResultVo.setField(SystemDictConstant.STOP_PERSON);
                    comparisonResultVo.setFieldNotes("停发");
                }else if(SystemDictConstant.CANCEL_STOP.equals(personOperate.getOperateCode())){
                    comparisonResultVo.setField(SystemDictConstant.CANCEL_STOP);
                    comparisonResultVo.setFieldNotes("续发");
                }
                Date date = null;
                try {
                    date = df.parse(personOperate.getCreateTime());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                comparisonResultVo.setStartDate(df.format(date));
                comparisonResultVo.setRemark(personOperate.getRemarks());
                comparisonResultVo.setCreatTime(personOperate.getCreateTime());
                resultVoList.add(comparisonResultVo);
            }
        }
        return resultVoList;
    }

    @Override
    public List<ComparisonResultVo> getPersonOperateRecord(String agencyId, String personId, String operateCode,String creatTime) {
        List<ComparisonResultVo> resultVoList = new ArrayList<>();
        List<PersonOperate> operateList=null;
        if("待调入".equals(operateCode)){
            operateList=iPersonOperateDao.getPersonOperatePersonState(agencyId,personId,operateCode);
        }else{
            // 获取人员变更记录
            operateList = iPersonOperateDao.getPersonOperateRecord(agencyId, personId, operateCode,creatTime);
        }

       cc: for (int i = 0; i < operateList.size(); i++) {
            PersonOperate personOperate = operateList.get(i);
            for (ComparisonResultVo comparisonResultVo1 : resultVoList) {
                if (personOperate.getOperateCode().equals(comparisonResultVo1.getField())) {
//                    isExist = true;
                    continue cc;
                }
            }
            ComparisonResultVo comparisonResultVo = new ComparisonResultVo();
            // 入职
            if (SystemDictConstant.ADD_PERSON.equals(personOperate.getOperateCode()) && operateCode.equals(personOperate.getOperateCode())) {
                comparisonResultVo.setField(SystemDictConstant.ADD_PERSON);
                comparisonResultVo.setFieldNotes("新增");
                resultVoList.add(comparisonResultVo);
            }
            // 调入
            else if (SystemDictConstant.IN_PERSON.equals(personOperate.getOperateCode()) && operateCode.equals(personOperate.getOperateCode())) {
                comparisonResultVo.setField(SystemDictConstant.IN_PERSON);
//                PersonOperate oldPersonOperate = operateList.get(i - 1);
                comparisonResultVo.setFieldNotes("调入");
                comparisonResultVo.setBefore(personOperate.getInFromAgency());
                comparisonResultVo.setAfter(personOperate.getAgencyId());
                resultVoList.add(comparisonResultVo);
            }
            //待调入
            else if("待调入".equals(operateCode)){
                comparisonResultVo.setField("ddr");
                comparisonResultVo.setFieldNotes("待调入");
                comparisonResultVo.setBefore(personOperate.getInFromAgency());
                //comparisonResultVo.setAfter(personOperate.getAgencyId());
                resultVoList.add(comparisonResultVo);
            }
            // 调出
            else if (SystemDictConstant.OUT_PERSON.equals(personOperate.getOperateCode()) && operateCode.equals(personOperate.getOperateCode())) {
                comparisonResultVo.setField(SystemDictConstant.OUT_PERSON);
                comparisonResultVo.setFieldNotes("调出");
                comparisonResultVo.setBefore(personOperate.getAgencyId());
                comparisonResultVo.setAfter(personOperate.getOutToAgency());
                resultVoList.add(comparisonResultVo);
            }
            // 消减
            else if (SystemDictConstant.DELETE_PERSON.equals(personOperate.getOperateCode()) && operateCode.equals(personOperate.getOperateCode())) {
                comparisonResultVo.setField(SystemDictConstant.DELETE_PERSON);
                comparisonResultVo.setFieldNotes("消减");
                resultVoList.add(comparisonResultVo);
            }
            // 停发
            else if (SystemDictConstant.STOP_PERSON.equals(personOperate.getOperateCode()) && operateCode.equals(personOperate.getOperateCode())) {
                comparisonResultVo.setField(SystemDictConstant.STOP_PERSON);
                comparisonResultVo.setFieldNotes("停发");
                resultVoList.add(comparisonResultVo);
            }
            // 续发
            else if (SystemDictConstant.CANCEL_STOP.equals(personOperate.getOperateCode()) && operateCode.equals(personOperate.getOperateCode())) {
                comparisonResultVo.setField(SystemDictConstant.CANCEL_STOP);
                comparisonResultVo.setFieldNotes("续发");
                resultVoList.add(comparisonResultVo);
            }
            // 编辑
            else if (SystemDictConstant.EDIT_PERSON.equals(personOperate.getOperateCode()) && operateCode.equals(personOperate.getOperateCode())) {
                PersonOperate oldPersonOperate = operateList.get(operateList.size()-1);
                try {
                    // 比对不一致的值
                    List<ComparisonResultVo> list = CompareUtils.compareObj(oldPersonOperate, personOperate);
                    for (ComparisonResultVo comparisonResultVo1 : list) {
                        if ("operateCode".equals(comparisonResultVo1.getField())||"createTime".equals(comparisonResultVo1.getField())||"idenIsVerify".equals(comparisonResultVo1.getField())||"idenVerifyState".equals(comparisonResultVo1.getField())
                               ||"inFromAgency".equals(comparisonResultVo1.getField()) ||"startDate".equals(comparisonResultVo1.getField())
                                ||"funcId".equals(comparisonResultVo1.getField())||"funcCode".equals(comparisonResultVo1.getField())) {
                            continue;
                        }
                        comparisonResultVo1.setFieldNotes(
                                PersonFiledEnum.valueOfField(comparisonResultVo1.getField()).getNotes());
                        resultVoList.add(comparisonResultVo1);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return resultVoList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void undoAuditPersonOperateList(List<PersonOperate> list) {
        for (PersonOperate personOperate : list) {
            personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            personOperate.setIdenVerifyState(SystemDictConstant.IS_SUBMIT);
            iPersonOperateDao.updatePersonOperate(personOperate);
            //通过人员id 查询就身份证
            PersonOperate oldIdenNo = iPersonOperateDao.getOldIdenNo(personOperate.getPersonId());
            //更新perseninfo
            PersonInfo personInfo = new PersonInfo();
            personInfo.setIdenNo(oldIdenNo.getIdenNo());
            personInfo.setId(personOperate.getPersonId());
            personInfo.setIdenIsVerify(personOperate.getIdenIsVerify());
            personInfo.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            iPersonInfoDao.updatePersonInfo(personInfo);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcelBankData(List<PersonOperate> list,UserDTO userDto,String agencyId) {
        List<Object[]> params = new ArrayList<>();
        String updateTime = String.valueOf(DateUtils.getCurrentTimeStamp());

        ConfigCtrl cc=iCommonService.selectConfigCtrlByTypeMof("fileVerify",userDto.getProvince());
        if(CollectionUtils.isEmpty(list)){
            throw  new AppRuntimeException("无可修改数据！");
        }
        String salary_month = agencyVerifyDao.getSalaryMonth(userDto.getProvince(),agencyId);

            /**通过身份证号查找人员信息START*/
            List<String> idenNoList=list.stream().map(PersonOperate::getIdenNo).collect(Collectors.toList());
            List<PersonOperate> personOperates=iPersonOperateDao.getPersonOperateCodeByIdenNos(idenNoList,salary_month,agencyId);
            if(personOperates.size()!=idenNoList.size()){
                throw new AppRuntimeException("存在身份证号无法识别人员");
            }
            Map<String,PersonOperate> poMap=new HashMap<>();
            Map<String,PersonOperate> poidenMap=new HashMap<>();
            for(PersonOperate po:personOperates){//将查到的信息转为MAP
                poMap.put(po.getSalaCardNo(),po);
                poidenMap.put(po.getIdenNo(),po);
            }
            for(PersonOperate po:list){//剔除银行卡号未修改的信息
                if(!StringUtils.isEmpty(poMap.get(po.getSalaCardNo()))){//不为空则表示没有修改，从数列中剔除
                    personOperates.remove(poMap.get(po.getSalaCardNo()));
                    poMap.remove(po.getSalaCardNo());
                    poidenMap.remove(po.getIdenNo());
                }else{
                    poidenMap.get(po.getIdenNo()).setSalaCardNo(po.getSalaCardNo());//修改对象银行卡号
                    poidenMap.get(po.getIdenNo()).setSalaCardBank(po.getSalaCardBank());
                }
            }
        /**通过身份证号查找人员信息END*/
        //校验附件是否存在
        if(cc==null||"1".equals(cc.getIsAble())){//等于空和1为校验
            Set<String>keySet=poMap.keySet();
            for(String key:keySet){
                if(StringUtils.isEmpty(poMap.get(key).getExistsFile())){
                    throw  new AppRuntimeException("修改的人员中，存在无附件人员！请上传附件");
                }
            }

        }

        // 根据身份证修改银行和卡号
        for (PersonOperate personOperate : personOperates) {
            Object[] objects = new Object[]{
                    personOperate.getSalaCardBank(), personOperate.getSalaCardNo(),updateTime, personOperate.getIdenNo(),
                    agencyId
            };
            params.add(objects);
        }
        // 批量更新人员表
        iPersonInfoDao.batchUpdateBank(params);
        //操作表新增修改人员记录
        iPersonOperateDao.batchInsertUpdate(personOperates);
        // 批量更新操作表
       // iPersonOperateDao.batchUpdateBank(params);
    }

    @Override
    public PersonOperate getOldPersonOperate (String personid){
        return  iPersonOperateDao.getOldIdenNo(personid);
    }

    @Override
    public PersonOperate getSalaryMonth(String agencyId) {
        return iPersonOperateDao.getSalaryMonth(agencyId);
    }
}
